package com.fire.web.modules.all.service.impl;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fire.web.modules.all.constant.ErrorCodeConstants;
import com.fire.web.modules.all.entity.AjaxResult;
import com.fire.web.modules.all.entity.FireDangrousResouceModel;
import com.fire.web.modules.all.mapper.FireDangrousResouceMapper;
import com.fire.web.modules.all.mapper.SequenceMapper;
import com.fire.web.modules.all.request.AddFireDangrousResouceReq;
import com.fire.web.modules.all.service.IFireDangrousResouceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 消防危险源Service
 *
 * @author Matthew
 * @Date 2019-01-17 20:48
 */
@Service
public class FireDangrousResouceServiceImpl extends ServiceImpl<FireDangrousResouceMapper, FireDangrousResouceModel> implements IFireDangrousResouceService {

	private static final Logger LOGGER = LoggerFactory.getLogger(FireDangrousResouceServiceImpl.class);

	@Autowired
	private FireDangrousResouceMapper fireDangrousResouceMapper;
	@Autowired
	private SequenceMapper sequenceMapper;

	@Override
	public FireDangrousResouceModel selectOne(FireDangrousResouceModel entity) {
		return fireDangrousResouceMapper.selectOne(entity);
	}

	@Override
	public List<FireDangrousResouceModel> selectList(FireDangrousResouceModel model) {
		return fireDangrousResouceMapper.selectListByModel(model);
	}

	@Override
	public Page<FireDangrousResouceModel> selectPage(int currentPage, int pageSize, Wrapper<FireDangrousResouceModel> wrapper) {
		return selectPage(new Page<FireDangrousResouceModel>(currentPage, pageSize), wrapper);
	}

	@Override
	public Page<Map<String, Object>> selectMapsPage(int currentPage, int pageSize, Wrapper<FireDangrousResouceModel> wrapper) {
		return selectMapsPage(new Page<FireDangrousResouceModel>(currentPage, pageSize), wrapper);
	}

	@Override
	public AjaxResult updateDangrousResouce(AddFireDangrousResouceReq req) {
		FireDangrousResouceModel fireDangrousResouceModel = new FireDangrousResouceModel();
		fireDangrousResouceModel.setAddress(req.getAddress());
		fireDangrousResouceModel.setCity(req.getCity());
		fireDangrousResouceModel.setDistrict(req.getDistrict());
		fireDangrousResouceModel.setDresname(req.getDresname());
		fireDangrousResouceModel.setDresourceid(req.getDresourceid());
		fireDangrousResouceModel.setDrestype(req.getDrestype());
		fireDangrousResouceModel.setLatitude(req.getLatitude());
		fireDangrousResouceModel.setLevation(req.getLevation());
		fireDangrousResouceModel.setLevel(req.getLevel());
		fireDangrousResouceModel.setLongitude(req.getLongitude());
		fireDangrousResouceModel.setProvince(req.getProvince());
		fireDangrousResouceModel.setSupervisorid(req.getSupervisorid());
		fireDangrousResouceModel.setSupervisorname(req.getSupervisorname());
		fireDangrousResouceModel.setUpdatetime(new Date());
		fireDangrousResouceModel.setZone(req.getZone());
		fireDangrousResouceMapper.updateAllColumnById(fireDangrousResouceModel);
		return new AjaxResult(ErrorCodeConstants.SUCCESS,"更新成功","更新成功");
	}

	@Override
	@Transactional
	public AjaxResult addDangrousResouce(AddFireDangrousResouceReq req){
		FireDangrousResouceModel fireDangrousResouceModel = new FireDangrousResouceModel();
		String dresId = sequenceMapper.getNextStringValue("dangrousResouce");
		dresId = dresId.substring(4);
		DecimalFormat df = new DecimalFormat("0000");
		dresId = df.format(Integer.parseInt(dresId));
		dresId = "Dres" + dresId;
		fireDangrousResouceModel.setDresourceid(dresId);
		fireDangrousResouceModel.setAddress(req.getAddress());
		fireDangrousResouceModel.setCity(req.getCity());
		fireDangrousResouceModel.setCreatetime(new Date());
		fireDangrousResouceModel.setDistrict(req.getDistrict());
		fireDangrousResouceModel.setDresname(req.getDresname());
		fireDangrousResouceModel.setDrestype(req.getDrestype());
		fireDangrousResouceModel.setLatitude(req.getLatitude());
		fireDangrousResouceModel.setLevation(req.getLevation());
		fireDangrousResouceModel.setLevel(req.getLevel());
		fireDangrousResouceModel.setLongitude(req.getLongitude());
		fireDangrousResouceModel.setProvince(req.getProvince());
		fireDangrousResouceModel.setSupervisorid(req.getSupervisorid());
		fireDangrousResouceModel.setSupervisorname(req.getSupervisorname());
		fireDangrousResouceModel.setUpdatetime(new Date());
		fireDangrousResouceModel.setZone(req.getZone());
		fireDangrousResouceMapper.insert(fireDangrousResouceModel);
		return new AjaxResult(ErrorCodeConstants.SUCCESS,"新增成功","新增成功");
	}

}
